package com.qfedu.java1234.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qfedu.java1234.entity.bo.Menu;
import com.qfedu.java1234.entity.bo.RoleMenu;
import com.qfedu.java1234.entity.vo.ResultVO;
import com.qfedu.java1234.service.MenuService;
import com.qfedu.java1234.service.RoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @ClassName MenuController
 * @Author ykx
 * @Date 2024/4/24 23:13
 * @Version 1.0
 */
@RestController
@RequestMapping("/sys/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @Autowired
    private RoleMenuService roleMenuService;

    /**
     * 查询所有菜单树信息
     *
     * @return
     */
    @RequestMapping("/list")
    @PreAuthorize("hasAuthority('system:menu:query')")
    public ResultVO list() {
        List<Menu> menuList = menuService.list(new QueryWrapper<Menu>().orderByAsc("order_num"));
        return ResultVO.success(menuService.buildTreeMenu(menuList));
    }


    /**
     * 更新角色权限信息
     *
     * @param id
     * @param menuIds
     * @return
     */
    @Transactional
    @PostMapping("/updateMenus/{id}")
    @PreAuthorize("hasAuthority('system:role:menu')")
    public ResultVO updateMenus(@PathVariable(value = "id") Long id, @RequestBody List<Long> menuIds) {
        roleMenuService.remove(new QueryWrapper<RoleMenu>().eq("role_id", id));
        List<RoleMenu> sysRoleMenuList = new ArrayList<>();
        menuIds.forEach(menuId -> {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(id);
            roleMenu.setMenuId(menuId);
            sysRoleMenuList.add(roleMenu);
        });
        roleMenuService.saveBatch(sysRoleMenuList);
        return ResultVO.success();
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    @PreAuthorize("hasAuthority('system:menu:delete')")
    public ResultVO delete(@PathVariable(value = "id") Long id) {
        int count = menuService.count(new QueryWrapper<Menu>().eq("parent_id", id));
        if (count > 0) {
            return ResultVO.failed("请先删除子菜单！");
        }
        menuService.removeById(id);
        return ResultVO.success();
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('system:menu:query')")
    public ResultVO findById(@PathVariable(value = "id") Long id) {
        Menu sysMenu = menuService.getById(id);
        Map<String, Object> map = new HashMap<>();
        map.put("sysMenu", sysMenu);
        return ResultVO.success(map);
    }

    /**
     * 添加或者修改
     *
     * @param sysMenu
     * @return
     */
    @PostMapping("/save")
    @PreAuthorize("hasAuthority('system:menu:add')" + "||" + "hasAuthority('system:menu:edit')")
    public ResultVO save(@RequestBody Menu sysMenu) {
        if (sysMenu.getId() == null || sysMenu.getId() == -1) {
            sysMenu.setCreateTime(new Date());
            menuService.save(sysMenu);
        } else {
            sysMenu.setUpdateTime(new Date());
            menuService.updateById(sysMenu);
        }
        return ResultVO.success();
    }
}
